<script setup lang="ts">
import { message } from '@/components/XtxUI';
import useStore from '@/store';
import type { QQUserInfo } from '@/types';
import { reactive } from 'vue';
import { useCountDown } from '@/hooks';
// 准备表单数据
const { unionId, userInfo } = defineProps<{
  userInfo: QQUserInfo;
  unionId: string;
}>();
const form = reactive({
  mobile: '13012345682',
  code: '123456',
});

const loginBind = () => {
  // 绑定登录
  const { member } = useStore();
  member.loginBind({
    unionId,
    mobile: form.mobile,
    code: form.code,
  });
};
const { member } = useStore();
const { start, count } = useCountDown();
// 发送验证码按钮
const sendCode = async () => {
  // 1. 进行手机号格式校验
  if (!/^1[3-9]\d{9}$/.test(form.mobile)) {
    // 校验不通过，提醒用户
    return message({ type: 'warn', text: '手机号码格式错误~' });
  }
  // 避免倒计时没结束，就重复调用接口
  if (count.value !== 0) return;
  // 开启60s倒计时
  start(60);

  // 2. 调用获取验证码接口
  await member.getSocialCode(form.mobile);
};
</script>

<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="userInfo?.data?.figureurl_qq" alt="" />
      <p>
        Hi，{{ userInfo?.data?.nickname }}
        欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="form.mobile"
          maxlength="11"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="form.code"
          maxlength="6"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" @click="sendCode">{{
          count === 0 ? '发送验证码' : count + 's'
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <a @click="loginBind" href="javascript:;" class="submit">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
